$(function () {
    getUSerInof();
});
//退出功能
let layer = layui.layer;
$('#btnLogout').on('click', function () {
    layer.confirm('是否确认退出?', {icon: 3, title:'提示'}, function(index){
        //清空本地token
        localStorage.removeItem('token');
        // 页面跳转
        location.href = '/login.html';
        // 关闭询问窗口
        layer.close(index);
      })
})



//获取用户信息(封装一个全局函数)
function getUSerInof() {
    $.ajax({

        url: '/my/userinfo',
        // headers: {
        //     Authorization: localStorage.getItem
        //         ('token') || ""
        // },
        success: function (res) {
            
            if (res.status != 0) return layui.layer.msg(res.message);
            
            // 渲染头像
            renderAvatar(res.data)
            
        },
    })

}





function renderAvatar(user) {
    let name = user.nickname || user.username;
    $('#welcome').html("欢迎&nbsp;&nbsp;" + name);
    if (user.user_pic != null) {
        //有头像
        $('.layui-nav-img').show().attr("src", user.user_pic);
        $('.text-avatar').hide();
    } else {
        // 没有头像
        $('.layui-nav-img').hide();
        let text = name[0].toUpperCase();
        $('.text-avatar').show().html(text);
    }
}
